<?php
/**
 * 首屏广告
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2019/6/3
 * @copyright PanxSoft Inc.
 */
namespace App\Http\Controllers\Admin\V1;


use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\V1\Manufacture\ManufactureDataRequest;
use App\Models\Manufacture;
use Illuminate\Http\Request;

class ManufactureController extends ApiController
{
    const ROUTES = [
        'index'   => 'admin.v1.Manufacture.index',
        'store'   => 'admin.v1.Manufacture.store',
        'update'  => 'admin.v1.Manufacture.update',
        'destroy' => 'admin.v1.Manufacture.destroy',
    ];

    public function index()
    {
        $manufactures = (new Manufacture)
            ->latest('created_at')
            ->paginate(\request()->get('per_page') ?: 15);

        return $this->response(compact('manufactures'));
    }

    public function store(ManufactureDataRequest $request)
    {
        $manufacture = (new Manufacture)->create($this->assembleStoreData($request));

        return $this->response(compact('manufacture'));
    }

    /**
     * @param ManufactureDataRequest $request
     * @param $key
     *
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(ManufactureDataRequest $request, $key)
    {
        $manufacture = (new Manufacture)->find($key);
            $this->checkItemExist($manufacture, trans('checker.admin.manufacture'));

        $updated = $manufacture->update($this->assembleStoreData($request));

        return $this->response(compact('manufacture', 'updated'));
    }

    public function destroy($key)
    {
        $deleted = (new Manufacture)->destroy($key);

        return $this->response(compact('deleted'));
    }

    /**
     * @param Request $request
     *
     * @return array
     */
    private function assembleStoreData(Request $request): array
    {
        return $request->only(['name', 'contact', 'telphone', 'address']);
    }
}